//主页房屋信息查询，多条件
$(function() {

	var IMAGE_BASE_URL = "http://123.207.146.44/house_images";// 访问地址，
	// 查询按钮
	$("#searchBtn").click(function() {
		var condition = $("#ico-search").val();// 获取用户输入的值
		$("#queryConditionSpan").html(condition);

		console.log("这句能执行？");
		conditionJson.condition = condition;
		console.log(conditionJson);
		$.queryHouse(conditionJson);
	})

	// 清空数据#listHouseUl上的数据
	$.clearUl = function() {
		$('#listHouseUl').html("");
	}

	// 遍历ul数据 #listHouseUl
	$.traverseTheData = function(json) {
		pageCount = json.pageCount;// 总页数
		$.page(pageCount, json.pageIndex);// 加载页码
		$("#totalRecord").text(json.totalRecord);// 设置查出的结果数
		var houses = json.houses;
		var photo = json.photos;
		$(houses)
				.each(
						function(index, element) {
							// index代表下标，element代表对应的内容
							// 在这里判断，赋值

							var imgsrc = IMAGE_BASE_URL
									+ photo[index].pPhotoUrl
									+ photo[index].pPhotoName;
							$('#listHouseUl')
									.append(
											"<li class='clearfix'>"
													+ "<a target='_blank' href='#'><img src='"
													+ imgsrc
													+ "' width='142' height='106'alt='' /></a>"
													+ "<div class='hide_border'><a target='_blank' href='../houseDetailHtml.action?hPhotoid="
													+ element.hPhotoid
													+ "'></a></div> "
													+ "<!--房源状态 start--> <!----> <!--房源状态 end-->"
													+ "<div class='list_title'>"
													+ "<div class='the_price'>"
													+ "<ol>"
													+ "<strong><span>￥"
													+ element.hRent
													+ "</span>/月</strong>"
													+ "</ol>"
													+ "</div>"
													+ "<h1><a target='_blank' href='#'>"
													+ element.hCaption
													+ "</a></h1>"
													+ "<p><span>"
													+ element.hSize
													+ "㎡</span>&nbsp;|&nbsp; <span>"
													+ element.hRoom
													+ "室</span>&nbsp; <span>"
													+ element.hHall
													+ "厅</span>&nbsp;<span>"
													+ element.hKitchen
													+ "厨</span>&nbsp; <span>"
													+ element.hBathroom
													+ "卫</span>&nbsp; |&nbsp; <span>"
													+ element.hHeading
													+ "</span>&nbsp;|&nbsp;<span>简装</span>&nbsp;|&nbsp;<span>"
													+ element.hFloor
													+ "层</span>&nbsp;|&nbsp;<span>"
													+ element.hRentalMethod
													+ "</span></p>"
													+ "<p class='house_hot'><span>"
													+ element.hCommunityName
													+ "<kbd class='mai-ico'></kbd>"
													+ element.hArea
													+ "&nbsp;"
													+ element.hRegion
													+ "&nbsp;"
													+ element.hLocation
													+ "</span></p>"
													+ "<dl class='clearfix'>"
													+ "<dd class='morel clearfix'>"
													+ "<!--租房特色 start/-->"
													+ "<label>首次出租</label"
													+ "><label>空房出租</label>"
													+ "<!--租房特色 end-->"
													+ "<!--租房配置 start-->"
													+ "<kbd class='mai-ico TV'>"
													+ "<samp class='three'>电视</samp>"
													+ "</kbd>"
													+ "<kbd class='mai-ico air_condition'>"
													+ "<samp class='three'>空调</samp>"
													+ "</kbd>"
													+ "<!--租房配置 end-->"
													+ "</dd>"
													+ "</dl>"
													+ "</div>" + "</li>");
						});
	}

	// 进入首页时间，展示所有数据
	$(function() {
		// $.getHouseAll();
		$.queryHouse(conditionJson);
	})

	$.getHouseAll = function() {
		$.ajax({
			type : "post",
			url : "/tenement/getHouseAll.action",
			contentType : "application/json;charset=utf-8",
			success : function(msg) {
				if (msg.houses != null) {
					$("#no_homeDiv").css("display", "none");
					$.traverseTheData(msg);
				} else {
					$("#no_homeDiv").css("display", "block");
				}
			}
		})
	}

	// 7个查询条件
	$.queryHouse = function(jsonData) {

		var a1DisValue = $("#A1").css("display");
		var a2DisValue = $("#A2").css("display");
		if (a1DisValue == "none" && a2DisValue == "none") {
			conditionJson.h_region = "不限";
		}
		console.log("134"+conditionJson.h_region+"  "+a1DisValue+"  "+a2DisValue);
		$.ajax({
			type : "post",
			url : "/tenement/queryHouse.action",
			data : JSON.stringify(jsonData),
			contentType : "application/json;charset=utf-8",
			success : function(msg) {
				$.clearUl();// 清除ul下的li重新加载数据
				if (msg.totalRecord != 0) {
					$("#noHomeDiv").css("display", "none");
					$.traverseTheData(msg);// 加载数据
				} else {
					$("#totalRecord").text(msg.totalRecord);// 设置查出的结果数
					$('#paging').html("");
					$("#noHomeDiv").css("display", "block");
				}
			}
		})
	}

	var conditionJson = {};// 7个查询条件
	// 第一个查询条件 h_area
	$("*[name='h_area']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).html();// 获取当前属性的值，获取的是String类型
		$("*[name='h_area']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		if (value == "海城区") {
			$("#A2").css("display", "none");
			$("#A1").css("display", "block");
		} else if (value == "银海区") {
			$("#A1").css("display", "none");
			$("#A2").css("display", "block");
		} else {
			$("#A1").css("display", "none");
			$("#A2").css("display", "none");
		}
		// console.log("conditionJson: "+conditionJson);
		conditionJson.h_area = value;
		console.log("属性 " + property + ":" + value);
		$.queryHouse(conditionJson);
		conditionJson.pageIndex = 1;
		console.log(conditionJson);
	});
	// 第二个查询条件 h_region
	$("*[name='h_region']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).html();// 获取当前属性的值，获取的是String类型
		$("*[name='h_region']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_region = value;
		console.log("属性 " + property + ":" + conditionJson.h_region);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});
	// 第三个查询条件h_rent
	$("*[name='h_rent']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).html();// 获取当前属性的值，获取的是String类型
		$("*[name='h_rent']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_rent = value;
		console.log("属性 " + property + ":" + conditionJson.h_rent);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});
	// 第四个查询条件h_room,户型几室
	$("*[name='h_room']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).attr("tabindex");// 获取当前属性的值，获取的是String类型
		$("*[name='h_room']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_room = value;
		console.log("属性 " + property + ":" + conditionJson.h_room);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});
	// 第五个查询条件,朝向h_heading
	$("*[name='h_heading']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).html();// 获取当前属性的值，获取的是String类型
		$("*[name='h_heading']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_heading = value;
		console.log("属性 " + property + ":" + conditionJson.h_heading);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});
	// 第六个查询条件,装修情况h_decoration
	$("*[name='h_decoration']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).attr("id");// 获取当前属性的值，获取的是String类型
		$("*[name='h_decoration']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_decoration = value;
		console.log("属性 " + property + ":" + conditionJson.h_decoration);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});
	// 第七个查询条件,出租方式h_rental_method
	$("*[name='h_rental_method']").click(function() {
		var property = $(this).attr("name");// 获取属性
		var value = $(this).html();// 获取当前属性的值，获取的是String类型
		$("*[name='h_rental_method']").removeAttr("style");// 移除样式背景色
		$(this).attr("style", "color: #fff; background-color: #f56140;");// 给当前操作的标签添加样式
		conditionJson.h_rental_method = value;
		console.log("属性 " + property + ":" + conditionJson.h_rental_method);
		console.log(conditionJson);
		conditionJson.pageIndex = 1;
		$.queryHouse(conditionJson);
	});

	// 操作样式默认排序，租金升序降序
	$("*[name='sortRent']").click(function() {
		var flag = $(this).attr("value");
		if (flag == "租金") {
			var iTip = $(this).find("i");
			var i = iTip.attr("class");
			var value = "";
			if (i == "mai-ico up") {
				iTip.attr("class", "mai-ico");
				value = "desc";
			} else {
				iTip.attr("class", "mai-ico up");
				value = "asc";
			}
			conditionJson.sort = value;// 租金排序
			conditionJson.pageIndex = 1;
			$.queryHouse(conditionJson);
			console.log(conditionJson);
		}

	});

	/*
	 * $(function() { })
	 */

	$.gotoPage = function(pageIndex) {
		$('#paging').html("");
		conditionJson.pageIndex = pageIndex;// 传入当前页码,默认pageSize为5条数据
		$.queryHouse(conditionJson);
		console.log(conditionJson);
	}
	var pageCount = 0;// 总页数为20 #paging
	$.page = function(pageCount, pageIndex) {
		$('#paging').html("");
		$("#pageIndexIpt").attr("value", pageIndex);
		var indexNum = parseInt(pageIndex);
		console.log("【当前页码indexNum】" + pageIndex);
		var pageIndex2 = pageIndex;// 样式控制，当前页码样式为class='on'

		// 加载首页和上一页a标签
		$('#paging').append(
				"<a href='javascript:$.gotoPage(1)' id='homePage'>首页</a>");
		$('#paging').append(
				"<a onclick='' class='up_page'><i class='mai-ico'></i>上一页</a>");

		if (pageIndex <= 3) {
			if (pageCount > 5) {
				for (var i = 1; i <= 5; i++) {
					$('#paging').append(
							"<a href='javascript:$.gotoPage(" + i
									+ ")' tabindex='" + i + "' name='page'>"
									+ i + "</a>");

					$("#paging a[tabindex=" + pageIndex + "]").addClass("on");// 设置样式on
				}
				$('#paging').append("<a>...</a>");
			} else {
				for (var i = 1; i <= pageCount; i++) {
					$('#paging').append(
							"<a href='javascript:$.gotoPage(" + i
									+ ")' tabindex='" + i + "' name='page'>"
									+ i + "</a>");

					$("#paging a[tabindex=" + pageIndex + "]").addClass("on");// 设置样式on
				}
			}
		} else if (4 <= pageIndex && pageIndex <= (pageCount - 5)) {
			$('#paging').append("<a>...</a>");
			// indexNum为中间数,显示前两个页码
			for (var i = 2; i >= 1; i--) {
				$('#paging').append(
						"<a href='javascript:$.gotoPage(" + (indexNum - i)
								+ ")' tabindex='" + (indexNum - i)
								+ "' name='page'>" + (indexNum - i) + "</a>");
			}
			// indexNum
			$('#paging').append(
					"<a href='javascript:$.gotoPage(" + indexNum
							+ ")' tabindex='" + indexNum + "' name='page'>"
							+ indexNum + "</a>");
			// indexNum为中间数,显示后两个页码
			for (var i = 1; i <= 2; i++) {
				$('#paging').append(
						"<a href='javascript:$.gotoPage(" + (indexNum + i)
								+ ")' tabindex='" + (indexNum + i)
								+ "' name='page'>" + (indexNum + i) + "</a>");
			}
			$('#paging').append("<a>...</a>");
			$("#paging a[tabindex=" + pageIndex + "]").addClass("on");// 设置样式on
		} else if ((pageCount - 5) < pageIndex && pageIndex <= pageCount) {
			console.log((pageCount - 5) < pageIndex && pageIndex <= pageCount);
			if (pageCount <= 5) {
				for (var i = 1; i <= pageCount; i++) {
					$('#paging').append(
							"<a href='javascript:$.gotoPage(" + i
									+ ")' tabindex='" + i + "' name='page'>"
									+ i + "</a>");

					$("#paging a[tabindex=" + pageIndex + "]").addClass("on");// 设置样式on
				}
			} else {
				$('#paging').append("<a>...</a>");
				for (var i = 5; i >= 1; i--) {
					$('#paging').append(
							"<a href='javascript:$.gotoPage("
									+ (pageCount - i + 1) + ")' tabindex='"
									+ (pageCount - i + 1) + "' name='page'>"
									+ (pageCount - i + 1) + "</a>");

					$("#paging a[tabindex=" + pageIndex + "]").addClass("on");// 设置样式on
				}
			}
		}

		// 加载下一页和尾页a标签
		$('#paging')
				.append(
						"<a onclick='' class='down_page'>下一页<i class='mai-ico'></i></a>");
		// 尾页a标签
		$('#paging').append(
				"<a href='javascript:$.gotoPage(" + pageCount
						+ ")' id='lastPage'>尾页</a>");

		// 绑定事件
		// 下一页
		$("#paging a[class='down_page']").click(function() {
			var pageIndex = $("#pageIndexIpt").attr("value");
			pageIndex = (parseInt(pageIndex) + 1);
			console.log("【点击当前页码】" + pageIndex);
			if (pageIndex > pageCount) {
				return;
			}
			$('#paging').html("");
			$.gotoPage(pageIndex);
		});
		// 上一页
		$("#paging a[class='up_page']").click(function() {
			var pageIndex = $("#pageIndexIpt").attr("value");
			pageIndex = (parseInt(pageIndex) - 1);
			console.log("【点击当前页码】" + pageIndex);
			if (pageIndex <= 0) {
				return;
			}
			$('#paging').html("");
			$.gotoPage(pageIndex);
		});
	}

})